import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-23
 * Time: 21:01
 */
public class Solution {
    public int romanToInt(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        map.put('I',1);
        map.put('V',5);
        map.put('X',10);
        map.put('L',50);
        map.put('C',100);
        map.put('D',500);
        map.put('M',1000);
        int ans = 0;
        for (int i = 0; i < s.length(); i++) {
            int cur = map.get(s.charAt(i));
            if (i < s.length() - 1) {
                int next = map.get(s.charAt(i+1));
                if (next > cur) {
                    ans += (next - cur);
                    i++;
                }else {
                    ans += cur;
                }
            }else {
                ans += cur;
            }
        }
        return ans;
    }
}
